package com.libraries.base;

import com.example.libraries.R;
import com.libraries.helper.DeviceHelper;
import com.libraries.helper.Logger;
import com.libraries.view.listener.ILoadDialogListener;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
/**
 * FragmentActivity基类
 * 定义统一回调接口
 * 生命周期监听
 */
@SuppressLint({ "NewApi", "ResourceAsColor" })
public class BaseFragmentActivity extends FragmentActivity implements ILoadDialogListener{
	private boolean visible;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		BaseApplication.registerListener(this);
		DeviceHelper.setStatusBarColor(this, R.color.status_bar);
		visible=true;
	}
	
	public boolean isVisible() {
		return visible;
	}

	@Override
	protected void onResume() {
		super.onResume();
		visible=true;
		Logger.log(this, "onResume");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		visible=false;
		Logger.log(this, "onPause");
//		Constants.onHomed=DeviceHelper.onHome(this);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		visible=false;
	}

	/**
	 * 界面已经不再可见的时候，我们应当将所有和界面相关的资源进行释放。在这种场景下释放资源可以让系统缓存后台进程的能力显著增加，
	 * 因此也会让用户体验变得更好。
	 */
	@Override
	public void onTrimMemory(int level) {
		super.onTrimMemory(level);
		Logger.log(this, "onTrimMemory", level);
		switch (level) {
		case TRIM_MEMORY_UI_HIDDEN:
			// 进行资源释放操作
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		Logger.log(this, "onActivityResult",arg0, arg1, arg2);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode==KeyEvent.KEYCODE_BACK) {
			doActivityResult(RESULT_CANCELED);
		}
		return super.onKeyDown(keyCode, event);
	}

	public void doActivityResult(int resultCode,Intent intent) {
		setResult(resultCode,intent);
		finish();
	}
	
	public void doActivityResult(int resultCode) {
		setResult(resultCode);
		finish();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Logger.log(this, "onDestroy");
//		BaseApplication.unregisterListener(this);
	}

	@Override
	public void dismiss(boolean interrupt) {
	}
}
